package com.lxl.fcexcel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lxl.fcexcel.listener.WebGoodsListener;
import com.lxl.fcexcel.pojo.Goods;
import com.lxl.fcexcel.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 北上寻海
 *
 * Scope("prototype"):这个过滤器需要是多例的
 */
@Slf4j
@Controller
@ResponseBody
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private WebGoodsListener webGoodsListener;

    @Autowired
    private GoodsService goodsService;
    /**
     * 将excel文件的数据读取写入数据库
     * @param excel 待读取的excel文件
     * @return Boolean
     */
    @RequestMapping("read")
    public Boolean readExcel(MultipartFile excel){
        try {
            //工作簿
            ExcelReaderBuilder readerBook = EasyExcel.read(excel.getInputStream(), Goods.class,webGoodsListener);

            //工作表,每读一次都调用对象的revoke方法
            readerBook.sheet().doRead();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 根绝时间段选择输出
     * @param start_date 开始时间
     * @param end_date 结束时间
     */
    @RequestMapping("/write")
    public void writeExcel( Long start_date,  Long end_date, String type, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(System.currentTimeMillis()/1000+"货物统计","utf-8");
        response.setHeader("Content-Disposition","attachment;filename*=UTF8''" + fileName + type);

        //模板
        String template = "file_data_template.xlsx";

        ServletOutputStream outputStream = response.getOutputStream();
        //工作簿对象
        ExcelWriterBuilder writerBook = EasyExcel.write(outputStream,Goods.class).withTemplate(template);
        //工作表对象
        ExcelWriterSheetBuilder sheet = writerBook.sheet();

        //查询数据
        //条件构造器
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //添加条件

        log.info("构造器对象:" + queryWrapper.toString());
        log.info(start_date.toString());
        log.info(end_date.toString());


        queryWrapper.ge(Goods::getTime,sdf.format(new Date(start_date)))
                    .le(Goods::getTime,sdf.format(new Date(end_date)));
        List<Goods> list = goodsService.list(queryWrapper);
        if(list != null && list.size()>0){
            sheet.doFill(list);
        }

    }
}
